define(["jquery","bbcare","fu/app/comm/comm"],function($,$bb,$comm){
	var servers = {};
	var URL = $bb.baseUrl("followup");

	/**
	 * 处理患者基本信息数据
	 */
	servers.handleInfo = function(info){
		var week = Math.floor( info.pregnancyDay / 7 );
		var day = info.pregnancyDay % 7;
		info.pregnancyWeek = week + "周" + day + "天";
		return info;
	}

	/**
	 * 问卷数据处理
	 */
	servers.handleQuestions = function(questions){
		for (var i = 0; i < questions.length; i++) {
			for (var j = 0; j < questions[i].question.length; j++) {
				questions[i].question[j].monthId = questions[i].id;
				questions[i].question[j].month = questions[i].month;
				questions[i].question[j].index = j;
				if( questions[i].question[j].imgList && questions[i].question[j].imgList.length > 0 ){
					var imgList = questions[i].question[j].imgList;
					var str = questions[i].question[j].standard;
					var arr = [];
					for (var p = 0; p < imgList.length; p++) {
						if( str.indexOf(imgList[p].sign) >= 0 ){
							var splitArr = str.split(imgList[p].sign);
							arr.push(splitArr[0]);
							arr.push(imgList[p]);
							str = splitArr[1];
							if( p == imgList.length - 1 ) arr.push(splitArr[1]);
						}
					}
					questions[i].question[j].standard = arr;
					delete questions[i].question[j].imgList;
				}
			}
		}
		return questions;
	}

	/**
	 * tab数据处理
	 */
	servers.handleTab = function(list){
		for (var i = 0; i < list.length; i++) {
			for (var j = 0; j < list[i].question.length; j++) {
				list[i].question[j].width = (100 / list[i].question.length) + "%";
			}
		}
		return {"list":list};
	}

	/**
	 * 根据月龄获取月龄id
	 */
	servers.getMonthIdBymonthAge = function(questions,monthAge){
		for (var i = 0; i < questions.length; i++) {
			if( i == questions.length - 1 ){
				if( parseInt(questions[i].month) < monthAge ){
					return questions[i].id;
				}
			}else{
				if( parseInt(questions[i].month) <= monthAge && parseInt(questions[i+1].month) > monthAge ){
					return questions[i].id;
				}
			}
		}
	}

	/**
	 * 获取问卷
	 */
	servers.getQuest = function(questions,monthId,id,answer){
		for (var i = 0; i < questions.length; i++) {
			if( questions[i].id == monthId ){
				for (var j = 0; j < questions[i].question.length; j++) {
					if( questions[i].question[j].id == id ){
						questions[i].question[j].answer = answer;
						return questions[i].question[j];
					}
				}
			}
		}
	}

	/**
	 * 获取情景问题组
	 */
	servers.getQuestGroup = function(questions,quest,resultArr){
		var arr = [];
		for (var i = 0; i < questions.length; i++) {
			for (var j = 0; j < questions[i].question.length; j++) {
				if( quest.group && questions[i].question[j].group == quest.group ){
					arr.push( questions[i].question[j] );
				}
			}
		}
		for (var i = 0; i < arr.length; i++) {
			for (var j = 0; j < resultArr.length; j++) {
				for (var p = 0; p < resultArr[j].question.length; p++) {
					if( arr[i].monthId == parseInt(resultArr[j].monthId) && arr[i].id == parseInt(resultArr[j].question[p].id) ){
						arr[i].answer = resultArr[j].question[p].result;
					}
				}
			}
		}
		return arr;
	}

	/**
	 * 获取答案
	 */
	servers.getResult = function(resultArr,monthId,id,result){
		var obj = {}, subObj = {};
		if( resultArr.length > 0 ){
			var flagA = false;
			for (var i = 0; i < resultArr.length; i++) {
				if( resultArr[i].monthId == monthId ){
					flagA = true;
					var flagB = false;
					for (var j = 0; j < resultArr[i].question.length; j++) {
						if( resultArr[i].question[j].id == id ){
							flagB = true;
							resultArr[i].question[j].result = result;
						}
					}
					if( !flagB ){
						subObj.id = id;
						subObj.result = result;
						resultArr[i].question.push(subObj);
					}
				}
			}
			if( !flagA ){
				obj.monthId = monthId;
				obj.question = [];
				subObj.id = id;
				subObj.result = result;
				obj.question.push(subObj);
				resultArr.push(obj);
			}
		}else{
			obj.monthId = monthId;
			obj.question = [];
			subObj.id = id;
			subObj.result = result;
			obj.question.push(subObj);
			resultArr.push(obj);
		}
		return resultArr;
	}

	/**
	 * 获取该月龄下所有题目是否“全通过”/“全失败”
	 * result: 
	 * 		参数为"1"时:返回true为全通过；false没有全通过
	 * 		参数为"0"时:返回true为全失败；false没有全失败
	 */
	servers.getMonthResult = function(questions,resultArr,index,result){
		for (var i = 0; i < questions.length; i++) {
			if( questions[i].id.toString() == resultArr[index].monthId ){
				if( resultArr[index].question.length < questions[i].question.length ){
					return false;
				}else{
					var flag = true;
					for( var j = 0; j < resultArr[index].question.length; j++){
						var answer = resultArr[index].question[j].result == result ? true : false;
						flag = flag && answer;
					}
					return flag;
				}
			}
		}
	}

	/**
	 * 获取下一个题目
	 */
	servers.getNextQuest = function(questions,resultArr,topMonthId,monthId,id){
		for (var i = 0; i < questions.length; i++) {
			if( questions[i].id.toString() == monthId ){
				for (var j = 0; j < questions[i].question.length; j++) {
					if( questions[i].question[j].id == id ){
						if( j + 1 <= questions[i].question.length - 1 ){ // 该月龄下还有题目时，跳下一个题目
							// i,j+1
							return {
								"monthIndex":i,
								"questIndex":j+1
							};
						}else{  // 该月龄下没有题目时，若已找到顶点，则往下跳；否则往上跳
							if( topMonthId ){
								if( topMonthId == monthId ){
									// Number(resultArr[0].monthId) - 1 + 1 , 0;  monthId比下标要大1，所以此处先减1获取下标，再加1，获取下一个下标
									return {
										"monthIndex":Number(resultArr[0].monthId) - 1 + 1, // ???
										"questIndex":0
									};
								}else{
									if( i >= questions.length - 1 ){
										return {
											"extremeBottom":true,
										};
									}else{
										return {
											"monthIndex":i+1,
											"questIndex":0
										};
									}
								}
							}else{
								// i-1,0  往上跳
								if( i > 0 ){
									return {
										"monthIndex":i-1,
										"questIndex":0
									}
								}else{ // 找到0月还没找到顶点，默认0月就是顶点，往回跳
									return {
										"monthIndex":Number(resultArr[0].monthId) - 1 + 1, // ???
										"questIndex":0,
										"extremeTop":true,
									};
								}
							}
						}
					}
				}
			}
		}
	}

	/**
	 * 处理结束提示框数据【根据能区造数据】
	 */
	servers.handleTypeData = function(type){
		var typeName,nextTypeName;
		switch(type){
			case "intelligence":
				typeName = "智力";
				nextTypeName = "社会适应";
				break;
			case "sociology":
				typeName = "社会适应";
				nextTypeName = "运动";
				break;
			case "sports":
				typeName = "运动";
				break;
		}
		return {
			"typeName":typeName,
			"nextTypeName":nextTypeName,
		}
	}

	/**
	 * 查询评估量表问卷 
	 */
	servers.ajaxPostion = function(params,callback){
		$bb.ajax({
			url: URL + "followup/service/patient/evaluation/getAllPostion",
			data:JSON.stringify({
				data : {
					"examinationPaperName": params.evaluationType,
					"userId":params.patientId || params.userId
				}
			}),
			success: function(data){
				if(data.success){
					callback(data.data,params);
				}else{
					$bb.toastShow(data.msg ? data.msg : "查询评估量表问卷出错！");
				}	
			}
		});
	}

	/**
	 * 查询用户信息
	 */
	servers.ajaxUserMassage = function(params,callback){
		$bb.ajax({
			url: URL + "followup/service/patient/patientInfoPtId",
			data:JSON.stringify({
				data : {
					"patientId": params.patientId || params.userId,
				}
			}),
			success: function(data){
				if(data.success){
					callback(data.data);
				}else{
					$bb.toastShow(data.msg ? data.msg : "查询用户信息出错！");
				}	
			}
		});
	}

	/**
	 * 评估提交
	 */
	servers.ajaxSubmitGetScore = function(params,callback){
		$bb.ajax({
			url: URL + "followup/service/patient/evaluation/getScore",
			data:JSON.stringify({
				data : params
			}),
			success: function(data){
				if(data.success){
					callback(data.data, params);
				}else{
					$bb.toastShow(data.msg ? data.msg : "评估提交出错！");
				}
			}
		});
	}

	return servers;
})